const express = require("express");
const app = express();
const port = 9000;
const fs = require("fs");
const path = require("path");
const resolve = (file) => path.resolve(__dirname, file);
const login =require("./server/api/login")
const tvInfo=require("./server/api/tvInfo")
const register=require("./server/api/register")
const session=require("express-session")
const MongoStore= require('connect-mongo')


app.set('trust proxy', 1)
// session
app.use(session({
  name:'captcha',
  secret:'12345678',
  saveUninitialized:true,
  cookie: { maxAge: 1000 * 60 * 60*24*30, secure: false },
  rolling: true,
  // 将session保存至数据库，设置24小时保存一次
  store:
    MongoStore.create({mongoUrl: 'mongodb://localhost:27017/zorvideo', touchAfter: 24 * 3600 })
  
}))
// post报文解析
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
// 基础路由
app.get('/',(req,res)=>{
  var html=fs.readFileSync(resolve("./dist/login.html"),'utf-8');
  res.send(html);
});
app.get('/login',(req,res)=>{
  var html=fs.readFileSync(resolve("./dist/login.html"),'utf-8');
  res.send(html);
});
app.get('/index',(req,res)=>{
  var html=fs.readFileSync(resolve("./dist/index.html"),'utf-8');
  res.send(html);
});

// 中间件
app.use('/api/register',register)
app.use('/api/login',login)
app.use('/api/tvInfo',tvInfo)
app.use(express.static(resolve("./dist")));



app.listen(port, () => {
  console.log(`Example app listening on port ${port}`);
});
